//
//  BudgetViewController.m
//  MarryMe
//
//  Created by lanou3g on 16/1/7.
//  Copyright © 2016年 revocher. All rights reserved.
//

#import "BudgetViewController.h"
#import "CalculateViewController.h"
@interface BudgetViewController ()<UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UITextField *moneyTextField;
@property (strong, nonatomic) IBOutlet UITextField *numberTextField;

@end

@implementation BudgetViewController


- (void)viewDidLoad {
    [super viewDidLoad];
     [self.navigationController setNavigationBarHidden:NO animated:YES];
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [customLab setTextColor:[UIColor redColor]];
    [customLab setText:@"预算管家"];
    customLab.font = [UIFont boldSystemFontOfSize:17];
    self.navigationItem.titleView = customLab;
    UIBarButtonItem *buttonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonItem:)];
    self.navigationItem.leftBarButtonItem=buttonItem;
    //代理
      self.numberTextField.delegate=self;
    self.moneyTextField.delegate=self;
    self.numberTextField.keyboardType=UIKeyboardTypeNumberPad;
    self.moneyTextField.keyboardType=UIKeyboardTypeNumberPad;

}





-(void)buttonItem:(UIBarButtonItem*)sender{

    [self.navigationController popToRootViewControllerAnimated:YES];
    


   }

- (IBAction)buttonDidCiled:(id)sender {
    if (self.numberTextField.text.length==0&&self.moneyTextField.text.length==0) {
    [self.moneyTextField becomeFirstResponder];
        UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:@"您还未输入信息" message:nil preferredStyle:UIAlertControllerStyleAlert];
        //模态推出
        [self presentViewController:alertVC animated:YES completion:nil];
        //延迟1秒
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        
        
    }
     
       if (self.numberTextField.text.length >0&&[self.moneyTextField.text intValue] <30000&&self.moneyTextField.text.length>0) {
           [self.numberTextField  resignFirstResponder];
           [self.moneyTextField becomeFirstResponder];
           UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:@"预算金额不能少于30000" message:nil preferredStyle:UIAlertControllerStyleAlert];
           //模态推出
           [self presentViewController:alertVC animated:YES completion:nil];
           //延迟2秒
           dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
               [self dismissViewControllerAnimated:YES completion:nil];
           });
      }

       if (  self.numberTextField.text.length >0&&[self.moneyTextField.text intValue]>30000)
    {
        
        CalculateViewController *calculate=[[CalculateViewController alloc]init];
        //隐藏tabBar
        [self setHidesBottomBarWhenPushed:YES];
        calculate.priceStr=self.moneyTextField.text;
        calculate.number=self.numberTextField.text;
        calculate.pice=self.moneyTextField.text;
        [self.navigationController pushViewController:calculate animated:YES];
 
          }
    if (self.numberTextField.text.length>0&&self.moneyTextField.text.length==0) {
        [ self.numberTextField  resignFirstResponder];
        [self.moneyTextField becomeFirstResponder];
        UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:@"您还未添加预算金额" message:nil preferredStyle:UIAlertControllerStyleAlert];
        //模态推出
        [self presentViewController:alertVC animated:YES completion:nil];
        //延迟2秒
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        
        
    }

    if ( self.moneyTextField.text.length>0&&self.numberTextField.text.length==0) {
        [ self.moneyTextField resignFirstResponder];
          [self.numberTextField becomeFirstResponder];
              UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:@"您还没添加桌数" message:nil preferredStyle:UIAlertControllerStyleAlert];
        //模态推出
        [self presentViewController:alertVC animated:YES completion:nil];
        //延迟2秒
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
    }
    
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if ( [ textField isEqual:self.moneyTextField]) {
        if(self.moneyTextField.text.length==0){
            if ([string isEqualToString:@"0"]) {
                return NO;
            }
        }
        
        if ( self.moneyTextField.text.length >= 6  &&  string.length >0)
        {
            return  NO;
        }
    }
    if ( [ textField isEqual: self.numberTextField]) {
        if(self.numberTextField.text.length==0){
            if ([string isEqualToString:@"0"]) {
                return NO;
            }
        }
    if ( self.numberTextField.text.length >= 3  &&  string.length >0)
    {
        return  NO;
    }
    }
       return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
